<script>
import { mapState } from 'vuex'
import { requestApi } from '@/requset/api'
import SearchManage from '@/components/basic/SearchManage.vue'
import { formConfig, config, column } from './formConfig.js'
import { deepClone } from '@/plugins/clone'
export default {
    extends: SearchManage,
    data() {
        return {
            table_title: '工单',
            url: 'notice',
            formConfig: null,
            tableColumn: null,
            tableData: [],
            queryForm: {
                scope_id: null,
                scope_id_list: [],
                status: null,
                device_name: '',
                start_time: '',
                end_time: '',
                page: 1,
                size: 20,
            },
            queryFormC: {
                scope_id: null,
                scope_id_list: [],
                status: null,
                device_name: '',
                start_time: '',
                end_time: '',
                page: 1,
                size: 20,
            },
        }
    },
    methods: {
        setQueryForm(queryForm, query) {
            let { warn_level, device_name, zone_id, factory_id, company_id, group_id, status } = query
            if (warn_level) this.queryForm.warn_level = warn_level
            if (device_name) this.queryForm.device_name = device_name
            if (status) this.queryForm.status = status
            let setList = (userLevel, list) => {
                if (userLevel === 'zone') {
                    return list.slice(3, 4)
                } else if (userLevel === 'factory') {
                    return list.slice(2, 4)
                } else if (userLevel === 'company') {
                    return list.slice(1, 4)
                } else {
                    return list
                }
            }
            if (zone_id) {
                this.$nextTick(() => {
                    queryForm.scope_id_list = setList(sessionStorage.getItem('userLevel'), [Number(group_id), Number(company_id), Number(factory_id), Number(zone_id)])
                    this.searchBtn()
                })
            } else this.searchBtn()
        },
        confirm(data) {
            this.$confirm('执行此操作后不可再修改工单，是否确定此工单无误', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(() => {
                    requestApi('post', this.url + '/edit', Object.assign(data, { status: 3 })).then((res) => {
                        if (res.data.code == 0) {
                            this.$message({
                                type: 'success',
                                message: '确认成功',
                            })
                            this.getlist()
                            this.$refs.zyform.closeDialog()
                        }
                    })
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消确认',
                    })
                })
        },
        edit(data) {
            requestApi('post', this.url + '/edit', this.to(deepClone(data))).then((res) => {
                if (res.data.code == 0) {
                    this.$message({
                        type: 'success',
                        message: '编辑成功',
                    })
                    this.$EventBus.$emit('undateWarn')
                    this.searchBtn()
                    this.$refs.zyform.closeDialog()
                }
            })
        },
    },
    computed: {
        ...mapState('backstage', ['backstageData']),
        tags() {
            if (this.stat !== null) {
                return [
                    { title: '工单数', num: this.pagination.total },
                    { title: '未处理', num: this.stat.unHandled },
                    { title: '已处理', num: this.stat.handled },
                    { title: '已确认', num: this.stat.confirmed },
                ]
            } else return []
        },
    },
    created() {
        let { is_handled, device_name } = this.$route.query
        if (is_handled) this.queryForm.is_handled = is_handled
        if (device_name) this.queryForm.device_name = device_name
        this.setQueryForm(this.queryForm, this.$route.query)
        formConfig[1].options = this.backstageData
        this.formConfig = formConfig
        this.dialog.config = config
        column[column.length - 1].buttons = [
            {
                text: '确认',
                click: (val) => this.confirm(val),
                if: (val) => val.status == '1',
                icon: 'el-icon-folder-checked',
            },
            {
                text: '处理',
                click: (val) => this.handleEdit(val),
                if: (val) => val.status == '2',
                icon: 'el-icon-edit',
            },
            {
                text: '修改',
                click: (val) => this.handleEdit(val),
                if: (val) => val.status == '1',
                icon: 'el-icon-edit',
            },
            {
                text: '删除',
                click: (val) => this.delete(val),
                if: () => this.operate,
                icon: 'el-icon-delete',
            },
        ]
        column[0].if = this.operate
        this.tableColumn = column
    },
}
</script>
